package com.example.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @author :create by Machong
 * 时间 2020-01-09 14
 */
public class TagView extends FrameLayout implements View.OnClickListener {
    private String mTitle;
    private String tags;
    private List<TagBean> tagArray;
    private TextView tvTitle;
    private FlexboxLayout flMain;
    private Map<String,Boolean> selecteMaps;

    public TagView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        inflate(context,R.layout.view_tag,this);
        initView();
        initArrts(context,attrs);
        setDefault();
    }

    private void setDefault() {
        setTitle(mTitle);
        setTags(tags);
    }




    private void initView() {
        tvTitle=findViewById(R.id.tv_title);
        flMain = findViewById(R.id.fl_tag);

    }

    private void initArrts(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TagView);
        mTitle = typedArray.getString(R.styleable.TagView_titleText);
        tags = typedArray.getString(R.styleable.TagView_tags);
    }

    private void setTitle(String mTitle) {
        if(TextUtils.isEmpty(mTitle)){
            return;
        }
        tvTitle.setText(mTitle);
        this.mTitle = mTitle;
    }

    public void setTags(String tags) {
        if(TextUtils.isEmpty(tags)){
            return;
        }
        setTags(formatFoArray(tags));
        notifyTag();
    }

    /**
     * 字符换格式为  name,key|name2,key2
     * @param tags
     * @return
     */
    private List<TagBean> formatFoArray(String tags) {
        String[] split = tags.split("\\|");
        List<TagBean> tbs= new ArrayList<>();
        for (String s : split) {
            tbs.add(new TagBean(s.split(",")[0],s.split(",")[1]));
        }
        return tbs;
    }

    public void setTags(List<TagBean> array){
        this.tagArray = array;
    }

    public void notifyTag(){
        flMain.removeAllViews();
        selecteMaps = new HashMap<>();
        selecteMaps.put("all",true);
        tvTitle.setTag("all");
        tvTitle.setOnClickListener(this);
        if(this.tagArray.isEmpty()){
            return;
        }
        for (TagBean tagBean : tagArray) {
            View inflate = LayoutInflater.from(getContext()).inflate(R.layout.view_tag_text, null);
            TextView tv = inflate.findViewById(R.id.tv_tag_name);
            tv.setText(tagBean.tagName);
            tv.setTag(tagBean.tagKey);
            selecteMaps.put(tagBean.tagKey,false);
            tv.setOnClickListener(this);
            flMain.addView(inflate);
        }
        notifyTagSelecte();
    }

    private void notifyTagSelecte() {
        TextView temp;
        setSelecteView(tvTitle,selecteMaps.get("all"));
        for (int i = 0; i < flMain.getChildCount(); i++) {
            temp=  flMain.getChildAt(i).findViewById(R.id.tv_tag_name);
            setSelecteView(temp,selecteMaps.get(temp.getTag()));
        }
    }

    private void setSelecteView(TextView tv,boolean is){
        if(is){
            tv.setBackgroundResource(R.color.colorAccent);
        }else{
            tv.setBackgroundResource(R.color.colorPrimary);
        }
    }

    @Override
    public void onClick(View view) {
        String tag = (String) view.getTag();
        Iterator<String> iterator = selecteMaps.keySet().iterator();
        while (iterator.hasNext()){
            selecteMaps.put(iterator.next(),false);
        }
        selecteMaps.put(tag,true);
        notifyTagSelecte();
    }

   public String getSelected(){
       Iterator<String> iterator = selecteMaps.keySet().iterator();
       StringBuffer sb=new StringBuffer();
       while (iterator.hasNext()){
           String next = iterator.next();
           if(selecteMaps.get(next)){
               sb.append(next);
               sb.append(",");
           }
       }
       return sb.toString();
   }


    class TagBean{
        private String tagName;
        private String tagKey;

        public TagBean(String tagName, String tagKey) {
            this.tagName = tagName;
            this.tagKey = tagKey;
        }
    }






}
